/**
 * 购物车
 * 2014-1-13
 */

CART = {};
CART.url = '';

jQuery().ready(function(){
	CART.url = jQuery('base').attr('href');
});

CART.loading = false;

CART.action = function(action,callback,data){
	if(CART.loading)return ;
	CART.loading = true;
	jQuery.ajax({
		type:'POST',
		url:CART.url+action,
		data:data,
		dataType:'json',
		success : function(res){
			callback(res);
		},
		error: function (data, status, e){
			
		},
		complete:function(){
			CART.loading = false;
		}
	});
}

CART.selPrice = function(id,tar){
	jQuery('#ticket_goumai .buy_etitcke').hide();
	jQuery('#price-hole a').removeClass('gouxuan');
	jQuery(tar).addClass('gouxuan');
	jQuery('#ticket_price_id').val(id);
	
	if($(tar).data('eticket')==1){
		var str = $(tar).data('eprice');
		var bstr = $(tar).data('price');
		str = (str/bstr).toFixed(2)*100;
		jQuery('#ticket_goumai .buy_etitcke').attr('title','购买电子票 享'+str+'折优惠').show();
	}
}

CART.changeCount = function(num){
	var $count = jQuery('#ticket_count');
	var scount = $count.val();
	var last = Number(scount)+Number(num);
	last=last>0?last:1;
	$count.val(last);
}

CART.ViewAddCart = function(type){
	var id = jQuery('#ticket_price_id').val();
	var count= jQuery('#ticket_count').val(); 
	if(!id){
		zmx.tip('请选择要购买的票价',false);
		zmx.shineTip();
		return false;
	}
	if(!count || count <=0){
		zmx.tip('请填写要购买的数量',false);
		zmx.shineTip();
		return false;
	}
	spt_eticket = 0;
	if(type == 1){
		var callback = function(res){
			location.href = CART.url+'cart_Carts_view.html';
		};
	}else if(type == 3){
		var callback = function(res){
			location.href = CART.url+'cart_Carts_view.html';
		};
		spt_eticket = 1;
	}else{
		var callback = function(res){
			zmx.tip(res.msg,true);
			zmx.shineTip();
			jQuery('#cart_count').html(res.data);
		};
	}
	CART.addToCart(id,count,callback,spt_eticket);	
};

CART.addToCart = function(id,count,callback,spt_eticket){
	var data = {
		id:id,
		count:count,
		param:spt_eticket
	}
	CART.action('cart/Carts/add/'+spt_eticket,callback,data);
};

CART.delItem = function(id){
	var data = {
		id:id
	}
	var callback = function(res){
		if(res.state){
			jQuery('#cart_count').html(res.data.count);
			jQuery('#v_price').html('¥'+res.data.price);
			jQuery('#v_count').html(res.data.count+'张');
			jQuery('#item-'+res.data.id).remove();
			if(res.data.count == 0){
				jQuery('#item-empty').show();
			}
		}else{
			alert(res.msg);
		}
	};
	CART.action('cart/Carts/del',callback,data);
};

CART.updateItem = function(id,tar){
	var data = {
		id:id,
		count:$(tar).val()
	}
	var callback = function(res){
		if(res.state){
			jQuery('#cart_count').html(res.data.count);
			jQuery('#v_price').html('¥'+res.data.price);
			jQuery('#v_count').html(res.data.count+'张');
		}else{
			zmx.tip(res.msg,false);
			zmx.shineTip();
		}
	};
	CART.action('cart/Carts/update',callback,data);
};

CART.updateEticket = function(id,tar){
	var spt_ticket = 0;
	if($(tar).is(':checked')){
		spt_ticket =1;
	}
	var data = {
		id:id,
		count:0,
		param:spt_ticket
	}
	var callback = function(res){
		if(res.state){
			jQuery('#cart_count').html(res.data.count);
			jQuery('#v_price').html('¥'+res.data.price);
			jQuery('#v_count').html(res.data.count+'张');
			if(spt_ticket){
				jQuery('#item-'+id+' .del_price').addClass('del_line');
			}else{
				jQuery('#item-'+id+' .del_price').removeClass('del_line');
			}
		}else{
			zmx.tip(res.msg,false);
			zmx.shineTip();
		}
	};
	CART.action('cart/Carts/update',callback,data);
};

CART.getCount = function(){
	var callback = function(res){
		jQuery('#cart_count').html(res.data);
	};
	CART.action('cart/Carts/count',callback);	
};

CART.askfor = function(ticket_price_id,tar){
	jQuery('#dialog').dialog({
		title:'缺票登记',
		autoOpen: true,
	    height: 400,
	    width: 450,
	    modal: true,
		resizeable:false,
		open: function( event, ui ) {
			price = jQuery('#ask_title').text()+'<br/>'+jQuery(tar).data('showtime')+' | '+jQuery(tar).text();
			jQuery('#ticket_price_show_title').html(price);
			
			jQuery('#ticket_price_ask_id').val(ticket_price_id);
		}
	});
};

CART.asksubmit = function(){
	var data = jQuery('#ticket_book').serializeObject(); 
	if(!CART.askcheck(data)){return false;}
	var callback = function(res){
		if(res.state){
			zmx.tip(res.msg,false);
			zmx.shineTip();
			jQuery('#dialog').dialog('close');
			jQuery('#TicketBookPhone,#TicketBookRemark').val('');
		}else{
			alert(res.msg);
		}
	};
	CART.action('TicketBooks/add',callback,data);
};
CART.askcheck = function(data){
	if(isNaN(data['data[TicketBook][count]'])||data['data[TicketBook][count]']<1){
		zmx.tip('数量不正确',false);
		zmx.shineTip();
		return false;
	};
	if(data['data[TicketBook][phone]'] == ''){
		zmx.tip('手机号码不正确',false);
		zmx.shineTip();
		return false;
	};
	return true;
} ;

CART.nologinTips = function(){
	var nouser = '<div id=nouser_confirm>';
	nouser +='您还没有登录<br/>用非会员通道购票，您将无法享受积分兑换、订单查询、等多项会员服务';
	nouser +='</div>';
	jQuery('body').append(nouser);
	$( "#nouser_confirm" ).dialog({
		title:'友情提示',
		resizable: false,
		height:200,
		autoOpen:false,
		modal: true,
	    buttons: {
	        "登录": function() {
	          location.href = 'Users_login.html';
	        },
	        "去结算": function() {
	          location.href = 'UserOrders_add.html';
	        }
		}
	});
}

CART.confirm = function(){
	$( "#nouser_confirm" ).dialog('open');
};

function AddFavorite(sURL,sTitle){
	try{
		window.external.addFavorite(sURL,sTitle);
	} catch(e) {
		try{
			window.sidebar.addPanel(sTitle,sURL,"");
		} catch(e) {
			zmx.tip('加入收藏失败，请使用Ctrl+D进行添加',false);
			zmx.shineTip();
		}
	}
}